package liuds.music;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import liuds.music.adapter.NewsAdapter;
import liuds.music.model.News;
import liuds.music.utils.Http;
import liuds.music.utils.Utils;

/**
 * Created by Administrator on 2018/5/24.
 */

@SuppressLint("ValidFragment")
public class NewsTypeFragment extends BaseFragment {

    public String title;
    private String type;

    private List<News> news = new ArrayList<>();
    private RecyclerView newsView;
    private NewsAdapter newsAdapter;

    public NewsTypeFragment(String title,String type) {
        super(title);
        this.type = type;
    }

    @Override
    protected void initView(Bundle state) {
        newsView = findViewById(R.id.newsView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        newsView.setLayoutManager(layoutManager);
    }

    @Override
    protected void loadData(final OnLoadDataListener listener){
        Http.getOther("http://v.juhe.cn/toutiao/index?type=" + type + "&key=65d4c89f2460e131bd8b288f3f70bff6", new Http.OnSuccessListener() {
            @Override
            public void onSuccess(Object result) {
                listener.onLoadSuccess();
                try {
                    JSONObject resultJson = new JSONObject(result.toString());
                    if (resultJson.optJSONObject("result") == null) {
                        Utils.toast(resultJson.optString("reason"));
                        return;
                    }
                    JSONArray newsJson = resultJson.optJSONObject("result").optJSONArray("data");

                    for (int i = 0; i < newsJson.length(); i++) {
                        news.add(new Gson().fromJson(newsJson.optJSONObject(i).toString(), News.class));
                    }
                    if (newsAdapter == null) {
                        newsAdapter = new NewsAdapter(getActivity(), news);
                        newsView.setAdapter(newsAdapter);
                        newsAdapter.setOnItemClickListener(new NewsAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view) {
                                Intent intent = new Intent(getActivity(), WebActivity.class);
                                News newsItem = news.get(newsView.getChildLayoutPosition(view));
                                intent.putExtra("url", newsItem.getUrl());
                                startActivity(intent);
                            }
                        });
                    } else {
                        newsAdapter.notifyDataSetChanged();
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.onLoadError(e.getMessage());
                }
            }
        }, new Http.OnErrorListener() {
            @Override
            public void onError(Object message) {
                listener.onLoadError(message.toString());
            }
        }, null);
    }

    @Override
    protected int getLayoutResouceId() {
        return R.layout.fragment_news_type;
    }
}
